/**
 * @Title: warningDeductionService_ctrl.js
 * @Description: 记分service
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author ex-xieyuwen001
 * @date 2014年12月29日 上午10:10:25
 * @version V1.0
 */

;(function(){
	'use strict';
	
	var app = angular.module('fwa.services');
	app.service('WarningDeductionService',['$http','BaseURL','WarningScreenService', function($http, baseUrl,WarningScreenService){
		var serv = this;
		var delFunc = function(datas,warningMarkData){
			angular.forEach(datas,function(dataTemps,indexTemp){
				if(!dataTemps.idPointCut && dataTemps.isFlat
						&& warningMarkData.caseNo == dataTemps.caseNo 
						&& warningMarkData.caseType != dataTemps.caseType){
					datas.splice(indexTemp,1);
				}
			   });
			return datas;
		}
		//记录删除已入库数据(公共)--start
		var deleteIndexTemp = function(datas,pointCutTemps,warningMarkData){
			if(pointCutTemps){
				angular.forEach(datas,function(data,index){
					angular.forEach(pointCutTemps,function(dataTemp,indexTemp){
							if(data.idPointCut == dataTemp.idPointCut && (data.idPointCut || data.storageIsTrue) && (dataTemp.idPointCut || dataTemp.storageIsTrue)
									&& warningMarkData.caseType == dataTemp.caseType 
									   && warningMarkData.caseNo === dataTemp.caseNo){
								pointCutTemps.splice(indexTemp,1);
							}
					   });
				   });
			}
			return pointCutTemps;
		}
		var deleteDedu = function(pointCutTemps,warningMarkData){
			if(pointCutTemps){
				var deduDels = warningMarkData.warningMarks[0].deductionDeletes;
				if(deduDels){
					pointCutTemps = deleteIndexTemp(deduDels,pointCutTemps,warningMarkData);
				}
			}
			return pointCutTemps;
		}
		//记录删除已入库数据(公共)--end
		//组装删除的已入库的数据
		var arrDeleteDeduction = function(data,warningMarkData,warningMarks){
			if(data.deductionDeletes){
				angular.forEach(data.deductionDeletes,function(datas,index){
					   if(warningMarkData.caseType == datas.caseType 
							   && warningMarkData.caseNo === datas.caseNo){
						   warningMarks.deductionDeletes.push(datas);
					   }
				   });
			}
		}
		//组装已操作数据
		var arrObjDeduction = function(data,deductionTempObjs,deductionObjs,warningMarkData){
			angular.forEach(deductionTempObjs,function(datas,index){
				   if(warningMarkData.caseType == datas.caseType 
						   && warningMarkData.caseNo === datas.caseNo){
					   deductionObjs.push(datas);
				   }else{
					   deductionObjs.push(datas);
				   }
			   });
			return deductionObjs;
		}
		//初始化记分数据
		serv.initDeduction = function(warningMarkData,warningMarks,tableDataDeductions,mark,targetType){
			if(!warningMarkData.warningMarks){
				tableDataDeductions = [];
			}else{
				var arr = [];
				if(warningMarkData.warningMarks){
					angular.forEach(warningMarkData.warningMarks,function(data,index){
						if(data.deductionDeletes){
							arrDeleteDeduction(data,warningMarkData,warningMarks);
						}
						if(mark == 1){
								if(index == 0){
									if(data.deducitonMedicals){
										arr = arrObjDeduction(data,data.deducitonMedicals,warningMarks.deducitonMedicals,warningMarkData);
									}
								}
						}else if(mark == 2){
								if(index == 0){
									if(data.deducitonStaffs){
										arr = arrObjDeduction(data,data.deducitonStaffs,warningMarks.deducitonStaffs,warningMarkData);
									}
								}
						}else if(mark == 3){
								if(index == 0){
									if(data.deducitonInsurs){
										arr = arrObjDeduction(data,data.deducitonInsurs,warningMarks.deducitonInsurs,warningMarkData);
									}
								}
						}
					});
				}
				if(arr){
					angular.forEach(deleteDedu(arr,warningMarkData),function(data,index){
						if(data.targetType == targetType 
								&& data.targetNo == warningMarkData.code
								&& warningMarkData.caseType == data.caseType 
								   && warningMarkData.caseNo === data.caseNo){
							tableDataDeductions.push(data);
						}
					   });
				}
			}
		}
		//清理暂存删除时遗留数据
		var deleteDedus = function(pointCutTemps,targetType){
			if(pointCutTemps){
				angular.forEach(pointCutTemps,function(datas,indexTemp){
					if(datas.delIsTrue && datas.storageIsTrue && !datas.idPointCut){
						pointCutTemps.splice(indexTemp,1);
					}
				   });
			}
			return pointCutTemps;
		}
		//初始校验值
		serv.intiParam = function(warningMarkData,targetNo,targetType,tableDataDeductions,pointCutTemps){
			var param = {
					caseType:warningMarkData.caseType,// 案件类型
					caseNo:warningMarkData.caseNo,// 案件编号
					targetType:targetType,// 扣分对象类型(01医疗机构、02医生、03参保人、04药店)
					targetNo:targetNo,// 扣分对象编号
					orgCode:warningMarkData.medicalCode// 机构编号
			};
			if(param){
				//------该对象总扣分值 start ------
				WarningScreenService.getPointCutTempScose(param).
				success(function(res){
					if(res.pointCutTemps){
						angular.forEach(deleteDedu(deleteDedus(res.pointCutTemps),warningMarkData),function(datas,index){
							if(datas.targetType == targetType 
									&& datas.targetNo == targetNo){
								if(datas.storageIsTrue){
									datas.isFlat = true;//暂存数据走新增操作数据校验流程
								}
								tableDataDeductions.push(datas);
								pointCutTemps.push(datas);
							}
						   });
					}
					return pointCutTemps;
				});
				//------该对象总扣分值 end ------
				return pointCutTemps;
			}
		}
		
		var toData = function(datas){
			 angular.forEach(datas,function(dataTemp,indexTemp){
				 if((dataTemp.idPointCut || dataTemp.storageIsTrue)){
					 datas.splice(indexTemp,1);
				 }
				});
			 return datas;
		 }
		
		//清理已入库及暂存数据
		serv.intiWarningMarks = function(warningMarks){
			 if(warningMarks){
				 angular.forEach(warningMarks,function(dataTemp,indexTemp){
					 dataTemp.deducitonMedicals = toData(dataTemp.deducitonMedicals);
					 dataTemp.deducitonStaffs = toData(dataTemp.deducitonStaffs);
					 dataTemp.deducitonInsurs = toData(dataTemp.deducitonInsurs);
//					 dataTemp.deductionDeletes = toData(dataTemp.deductionDeletes);
					});
			 }
			return warningMarks;
		}
		//获取操作数据
		serv.intiWarningMarkData = function(warningMarkData){
			warningMarkData.warningMarks = serv.intiWarningMarks(warningMarkData.warningMarks);
			return warningMarkData;
		}
		
		//先拆数据进行封装
		serv.warningMarkFunction = function(warningMarkData,warningMarks,mark,tableDataDeductions){
			angular.forEach(warningMarkData.warningMarks,function(data,index){
				if(index == 0){
					if(data.deducitonMedicals){
						angular.forEach(warningMarks.deductionDeletes,function(datas,indexDelTemp){
							angular.forEach(data.deducitonMedicals,function(dataTemps,indexTemp){
								if(datas.isFlat && datas.ruleId == dataTemps.ruleId
										&& datas.caseNo == dataTemps.caseNo && datas.caseType == dataTemps.caseType ){
									data.deducitonMedicals.splice(indexTemp,1);
								}
							   });
						   });
						data.deducitonMedicals = delFunc(data.deducitonMedicals,warningMarkData);
						warningMarks.deducitonMedicals = data.deducitonMedicals;
					}
					if(data.deducitonStaffs){
						angular.forEach(warningMarks.deductionDeletes,function(datas,indexDelTemp){
							angular.forEach(data.deducitonStaffs,function(dataTemps,indexTemp){
								if(datas.isFlat && datas.ruleId == dataTemps.ruleId
										&& datas.caseNo == dataTemps.caseNo && datas.caseType == dataTemps.caseType ){
									data.deducitonStaffs.splice(indexTemp,1);
								}
							   });
						   });
						data.deducitonStaffs = delFunc(data.deducitonStaffs,warningMarkData);
						warningMarks.deducitonStaffs = data.deducitonStaffs;
					}
					if(data.deducitonInsurs){
						angular.forEach(warningMarks.deductionDeletes,function(datas,indexDelTemp){
							angular.forEach(data.deducitonInsurs,function(dataTemps,indexTemp){
								if(datas.isFlat && datas.ruleId == dataTemps.ruleId
										&& datas.caseNo == dataTemps.caseNo && datas.caseType == dataTemps.caseType ){
									data.deducitonInsurs.splice(indexTemp,1);
								}
							   });
						   });
						data.deducitonInsurs = delFunc(data.deducitonInsurs,warningMarkData);
						warningMarks.deducitonInsurs = data.deducitonInsurs;
					}
					if(warningMarks.deductionDeletes){
						angular.forEach(warningMarks.deductionDeletes,function(datas,indexTemp){
							if(datas.isFlat && datas.storageIsTrue){
								data.deductionDeletes = warningMarks.deductionDeletes;
							}else if(datas.isFlat && !datas.storageIsTrue){
								warningMarks.deductionDeletes.splice(indexTemp,1);
							}else if(!datas.isFlat && !datas.storageIsTrue){
								if(data.deductionDeletes){
									angular.forEach(data.deductionDeletes,function(datasDel,indexDelTemp){
										if(!datas.isFlat && datas.idPointCut == datasDel.idPointCut){
											data.deductionDeletes.splice(indexDelTemp,1);
										}
									   });
//									warningMarks.deductionDeletes = data.deductionDeletes;
								}
								data.deductionDeletes.push(datas);
							}
						   });
						warningMarks.deductionDeletes = data.deductionDeletes;
					}
				}
			   });
			
			if(mark == 1){
				angular.forEach(tableDataDeductions,function(datas,index){
					angular.forEach(warningMarks.deducitonMedicals,function(data,indexTemp){
						if(datas.ruleId === data.ruleId 
								   && warningMarkData.code === data.targetNo){
							warningMarks.deducitonMedicals.splice(indexTemp,1);
						}
					   });
					if(datas.isFlat){
						warningMarks.deducitonMedicals.push(datas);
					}
				   });
			}else if(mark == 2){
				angular.forEach(tableDataDeductions,function(datas,index){
					angular.forEach(warningMarks.deducitonStaffs,function(data,indexTemp){
						if(datas.ruleId === data.ruleId
								   && warningMarkData.code === data.targetNo){
							warningMarks.deducitonStaffs.splice(indexTemp,1);
						}
					   });
					if(datas.isFlat){
						warningMarks.deducitonStaffs.push(datas);
					}
				   });
			}else if(mark == 3){
				angular.forEach(tableDataDeductions,function(datas,index){
					angular.forEach(warningMarks.deducitonInsurs,function(data,indexTemp){
						if(datas.ruleId === data.ruleId
								   && warningMarkData.code === data.targetNo){
							warningMarks.deducitonInsurs.splice(indexTemp,1);
						}
					   });
					if(datas.isFlat){
						warningMarks.deducitonInsurs.push(datas);
					}
				   });
			}
			return warningMarks;
		}
		
		//删除已入库的数据
		var deletePointCutTemp = function(datas,tableDataDeductions){
			angular.forEach(datas,function(data,index){
				angular.forEach(tableDataDeductions,function(dataTemp,indexTemp){
					if(data.ruleId === dataTemp.ruleId && dataTemp.idPointCut === data.idPointCut 
							&& (dataTemp.idPointCut || data.storageIsTrue)){
						 tableDataDeductions.splice(indexTemp,1);
					}
				   });
			   });
		}
		serv.deleteFunction = function(pointCutTemps,tableDataDeductions){
			if(pointCutTemps){//删除已扣过分的数据
				deletePointCutTemp(pointCutTemps,tableDataDeductions);
			}
			return true;
		}
		//组装删除数据
		serv.delWarningMarks = function(warningMarks,mark,data,ruleId,idPointCut){
			if(mark == 1){
				angular.forEach(warningMarks.deducitonMedicals,function(datas,indexMedical){
					if(data.ruleId === ruleId && datas.ruleId === data.ruleId && data.code === datas.code && data.isFlat){
						warningMarks.deducitonMedicals.splice(indexMedical,1);
					}
					if(data.ruleId === ruleId && datas.ruleId === data.ruleId && data.code === datas.code && !data.isFlat 
							&& (idPointCut === data.idPointCut || data.storageIsTrue)){
						warningMarks.deducitonMedicals.splice(indexMedical,1);
					}
				   });
		}else if(mark == 2){
				angular.forEach(warningMarks.deducitonStaffs,function(datas,indexStaff){
					if(data.ruleId === ruleId && datas.ruleId === data.ruleId && data.code === datas.code && data.isFlat){
						warningMarks.deducitonStaffs.splice(indexStaff,1);
					}
					if(data.ruleId === ruleId && datas.ruleId === data.ruleId && data.code === datas.code && !data.isFlat 
							&& (idPointCut === data.idPointCut || data.storageIsTrue)){
						warningMarks.deducitonStaffs.splice(indexStaff,1);
					}
				   });
		}else if(mark == 3){
				angular.forEach(warningMarks.deducitonInsurs,function(datas,indexInsurs){
					if(data.ruleId === ruleId && datas.ruleId === data.ruleId && data.code === datas.code && data.isFlat){
						warningMarks.deducitonInsurs.splice(indexInsurs,1);
					}
					if(data.ruleId === ruleId && datas.ruleId === data.ruleId && data.code === datas.code && !data.isFlat 
							&& (idPointCut === data.idPointCut  || data.storageIsTrue)){
						warningMarks.deducitonInsurs.splice(indexInsurs,1);
					}
				   });
		}
			return warningMarks;
		}
	}]);
	
})();